package aaa.schedule;

import java.util.ArrayList;

import aaa.database.Database;
import aaa.main.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ScheduleListAdapter extends BaseAdapter {
	
	public static final int[] ICON_ID = {
		R.drawable.status_other,
		R.drawable.status_not_updated,
		R.drawable.status_waiting_for_update,
		R.drawable.status_updating,
		R.drawable.status_updated
	} ;

	protected ArrayList<String> scheduleNames ;
	
	protected LayoutInflater inflater ;
	
	protected Database db ;

	public ScheduleListAdapter(Context context, Database db, String[] scheduleNames) {
		super() ;
		this.initSchedules(scheduleNames) ;
		this.inflater = LayoutInflater.from(context) ;
		this.db = db ;
	}

	public int getCount() {
		return this.scheduleNames.size() ;
	}

	public String getItem(int position) {
		return this.scheduleNames.get(position) ;
	}

	public long getItemId(int position) {
		return position ;
	}

	public View getView(int position, View contentView, ViewGroup parent) {
		View v = contentView ;
		if (v == null) {
			v = this.inflater.inflate(R.layout.schedule_listitem, null) ;
		}// if
		
		String scheduleName = this.scheduleNames.get(position) ;
		TextView name = (TextView) v.findViewById(R.id.schedule_name) ;
		name.setText(scheduleName) ;
		ImageView icon = (ImageView) v.findViewById(R.id.schedule_status) ;
		icon.setImageResource(ScheduleListAdapter.ICON_ID[this.db.getScheduleStatus(scheduleName)]) ;
		
		return v ;
	}

	public void setSchedules(String[] scheduleNames) {
		this.initSchedules(scheduleNames) ;
		this.notifyDataSetChanged() ;
	}
	
	public void clear() {
		this.scheduleNames.clear() ;
	}

	protected void initSchedules(String[] names) {
		this.scheduleNames = new ArrayList<String>(names.length) ;
		for(String name : names) {
			this.scheduleNames.add(name) ;
		}// for
	}
	
	public void removeSchedule(String scheduleName) {
		this.db.removeScheduleWithName(scheduleName) ;
		this.scheduleNames.remove(scheduleName) ;
		this.notifyDataSetChanged() ;
	}

}
